package com.wm.blog_web.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_common.base.BeanUtils;
import com.wm.blog_common.bean.PageHelper;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TRecommendDO;
import com.wm.blog_common.entity.TBlog;
import com.wm.blog_common.entity.TRecommend;
import com.wm.blog_common.result.Result;
import com.wm.blog_web.dao.TBlogDAO;
import com.wm.blog_web.dao.TRecommendDAO;
import com.wm.blog_web.query.TRecommendQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 推荐 服务实现类
 * </p>
 *
 * @author 半卷流年
 * @since 2020-04-03
 */
@Service
public class TRecommendService{
    @Autowired
    private TRecommendDAO tRecommendDAO;


    @Autowired
    private TBlogDAO tBlogDAO;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Result<TRecommendDO> selectById(Long id){
        return Result.success(BeanUtils.copyProperties(tRecommendDAO.getById(id),TRecommendDO.class));
    }

    /**
    * 根据ID删除
    * @param id
    * @return
    */
    public Result deleteById(Long id){
        return tRecommendDAO.removeById(id)?Result.success():Result.failure();
    }

    /**
    * 添加
    * @param tRecommend
    * @return
    */
    public Result insert(TRecommend tRecommend){
        return tRecommendDAO.save(tRecommend)?Result.success():Result.failure();
    }

    /**
    * 修改
    * @param tRecommend
    * @return
    */
    public Result update(TRecommend tRecommend){
        return tRecommendDAO.updateById(tRecommend)?Result.success():Result.failure();
    }

    /**
    * 统计总数
    *
    * @param query
    * @return
    */
    public Result<Integer> count(TRecommendQuery query) {
        return Result.success(tRecommendDAO.count(this.combineQuery(query)));
    }

    /**
    * 列表查询
    * @param query
    * @return
    */
    public Result<List<TRecommendDO>> list(TRecommendQuery query){
        return Result.success(BeanUtils.copyListProperties(tRecommendDAO.list(this.combineQuery(query)),TRecommendDO.class));
    }
    /**
    * 分页查询
    * @param page
    * @return
    */
    public Result<Page<TRecommendDO>> page(QueryPage page, TRecommendQuery query){
        return Result.success(PageHelper.newPage(tRecommendDAO.page(page,this.combineQuery(query)),TRecommendDO.class));
    }

    /**
    * 组装查询条件
    *
    * @param query
    * @return
    */
    private QueryWrapper<TRecommend> combineQuery(TRecommendQuery query) {
        QueryWrapper<TRecommend> wrapper = new QueryWrapper<>();
        return wrapper;
    }


    /**
     * 拉取两条推荐博文
     * @return
     */
    public Result hotRecommend(Integer current,Integer size) {
        size = Optional.ofNullable(size).orElse(5);
        current = Optional.ofNullable(current).orElse(1);
        QueryPage page = new QueryPage(current, size);
        Result<Page<TRecommendDO>> pageResult = this.page(page, new TRecommendQuery());
        Page newPage = PageHelper.newPage(pageResult.getData());
        if(pageResult.isSuccess() && !ObjectUtils.isEmpty(pageResult.getData()) &&  !CollectionUtils.isEmpty(pageResult.getData().getRecords())){
            List<TRecommendDO> doList = pageResult.getData().getRecords();
            List<Long> idList = doList
                    .stream()
                    .map(TRecommendDO::getRecommendId)
                    .distinct()
                    .collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(idList)){
                //查询博文信息
                QueryWrapper<TBlog> queryWrapper = new QueryWrapper<>();
                queryWrapper.in("id",idList);
                //点击率倒序
                queryWrapper.orderByDesc("click_count");
                List<TBlog> blogList = Optional
                        .ofNullable(tBlogDAO.list(queryWrapper))
                        .orElse(Collections.emptyList());
                newPage.setRecords(blogList);
                return Result.success(newPage);
            }
        }
        return Result.success(newPage);
    }
}







